package com.qidianit.mes.device.service.impl;

import com.github.pagehelper.PageHelper;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.pojo.DeviceTypeVO;
import com.qidianit.mes.pojo.Process;
import com.qidianit.mes.device.mapper.DeviceTypeMapper;
import com.qidianit.mes.device.service.DeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author djl
 * @Time 2024/10/15 23:28
 */
@Service
public class DeviceTypeServiceImpl implements DeviceTypeService {
    @Autowired
    private DeviceTypeMapper deviceTypeMapper;
    @Override
    public MESResult queryProcess(String deviceTypeName,Boolean isEnabled,String parentCode,Integer page, Integer limit) {
        // 开启分页  参数1： 页码  参数2： 每页大小
        PageHelper.startPage(page,limit);
        // 查询全部
        List<DeviceTypeVO> processes = deviceTypeMapper.selectlist(deviceTypeName,isEnabled,parentCode);
        // 将pageInfo对象设置到qdResult中
        return MESResult.success(processes);
    }

    @Override
    public MESResult addDeviceType(DeviceTypeVO deviceTypeVO) {
        if(deviceTypeMapper.selectByDeviceTypeId(deviceTypeVO.getDeviceTypeCode())== null){
            deviceTypeVO.setDeviceTypeStatus(1);
            deviceTypeMapper.addNewDeviceType(deviceTypeVO);
            return MESResult.success(Constant.RESPONSE_STATUS_OK);
        }
        return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);

    }

    @Override
    public MESResult editDeviceType(DeviceTypeVO deviceTypeVO) {

        deviceTypeMapper.editDeviceType(deviceTypeVO);
         return MESResult.success(Constant.RESPONSE_STATUS_OK);
    }

    @Override
    public MESResult deletedeviceType(String deviceTypeCode) {
        deviceTypeMapper.deleteDeviceType(deviceTypeCode);
        DeviceTypeVO deviceTypeVO = deviceTypeMapper.selectByDeviceTypeId(deviceTypeCode);
        String parentCode = deviceTypeVO.getParentCode();
        DeviceTypeVO deviceTypeFather = deviceTypeMapper.selectByDeviceTypeId(parentCode);

        Integer sum = deviceTypeMapper.selectByDeviceTypeSonCount(parentCode);
        if(sum == 0 ){
            deviceTypeFather.setParent(false);
            deviceTypeMapper.editDeviceType(deviceTypeFather);
        }
        return  MESResult.success(Constant.RESPONSE_STATUS_OK);
    }

    @Override
    public MESResult selectByParentCode(String parentCode) {
        List<DeviceTypeVO>deviceTypeVOS = deviceTypeMapper.selectByParentCode(parentCode);

        return MESResult.success(deviceTypeVOS);
    }

    @Override
    public MESResult addDeviceTypeSon(DeviceTypeVO deviceTypeVO, String parentCode) {
        //修改父类的状态
        DeviceTypeVO deviceTypeVO1 = deviceTypeMapper.selectByDeviceTypeId(parentCode);
        deviceTypeVO1.setParent(true);
        deviceTypeMapper.editDeviceType(deviceTypeVO1);

        deviceTypeVO.setDeviceTypeStatus(1);
        deviceTypeVO.setParentCode(parentCode);
        deviceTypeVO.setParent(false);
        deviceTypeMapper.addNewDeviceType(deviceTypeVO);


        return MESResult.success(Constant.RESPONSE_STATUS_OK);
    }
}
